package com.controller;

import com.domain.Hero;
import com.domain.Sky;
import com.images.Photo;
import com.service.BulletService;
import com.service.FlyingsService;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class ShootGame extends JPanel{

    public final static int WIDTH = 400;
    public final static int HEIGHT = 700;

    public static BufferedImage airplane0;//声明了图片类型的属性，存储敌机图片
    public static BufferedImage airplane1;//声明了图片类型的属性，存储敌机图片
    public static BufferedImage bigairplane0;//声明了图片类型的属性，存储敌机图片
    public static BufferedImage bigairplane1;//声明了图片类型的属性，存储敌机图片
    public static BufferedImage background;//声明了图片类型的属性，存储背景图片
    public static BufferedImage bee0;//声明了图片类型的属性，存储蜜蜂图片
    public static BufferedImage bee1;//声明了图片类型的属性，存储蜜蜂图片
    public static BufferedImage bullet;//声明了图片类型的属性，存储子弹图片
    public static BufferedImage gameover;//声明了图片类型的属性，存储游戏结束图片
    public static BufferedImage hero0;//声明了图片类型的属性，存储英雄机0图片
    public static BufferedImage hero1;//声明了图片类型的属性，存储英雄机1图片
    public static BufferedImage pause;//声明了图片类型的属性，存储游戏暂停图片
    public static BufferedImage start;//声明了图片类型的属性，存储游戏开始图片
    public static BufferedImage bom1;//声明了图片类型的属性，存储爆炸图片
    public static BufferedImage bom2;//声明了图片类型的属性，存储爆炸图片
    public static BufferedImage bom3;//声明了图片类型的属性，存储爆炸图片
    public static BufferedImage bom4;//声明了图片类型的属性，存储爆炸图片

    public static int score;//获得的分数
    public static int difficulty;//控制难度

    private Sky sky = new Sky();//创建天空
    private Hero hero = new Hero();//创建英雄机对象
    private BulletService bulletService;//创建子弹类
    private FlyingsService flyingsService;//声明一个存储敌机和蜜蜂的数组
    private boolean isPause = false; //判断是否暂停

    static {
        try {
            airplane0 = ImageIO.read(Photo.class.getResource("airplane0.png"));
            airplane1 = ImageIO.read(Photo.class.getResource("airplane1.png"));
            bigairplane0 = ImageIO.read(Photo.class.getResource("bigairplane0.png"));
            bigairplane1 = ImageIO.read(Photo.class.getResource("bigairplane1.png"));
            background = ImageIO.read(Photo.class.getResource("background.png"));
            bee0 = ImageIO.read(Photo.class.getResource("bee0.png"));
            bee1 = ImageIO.read(Photo.class.getResource("bee1.png"));
            bullet = ImageIO.read(Photo.class.getResource("bullet.png"));
            gameover = ImageIO.read(Photo.class.getResource("gameover.png"));
            hero0 = ImageIO.read(Photo.class.getResource("hero0.png"));
            hero1 = ImageIO.read(Photo.class.getResource("hero1.png"));
            pause = ImageIO.read(Photo.class.getResource("pause.png"));
            start = ImageIO.read(Photo.class.getResource("start.png"));
            bom1 = ImageIO.read(Photo.class.getResource("bom1.png"));
            bom2 = ImageIO.read(Photo.class.getResource("bom2.png"));
            bom3 = ImageIO.read(Photo.class.getResource("bom3.png"));
            bom4 = ImageIO.read(Photo.class.getResource("bom4.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ShootGame() {
        addMouseListener(new MouseAdapter() {
            //监听鼠标进入窗体事件
            @Override
            public void mouseEntered(MouseEvent e) {
                isPause = false;//结束暂停
            }
            //监听鼠标离开窗体事件
            @Override
            public void mouseExited(MouseEvent e) {
                isPause = true;//暂停
            }
            //监听鼠标双击事件
            @Override
            public void mouseClicked(MouseEvent e) {
                //判断鼠标双击，判断飞行物集合和子弹集合是否创建，判断英雄机血量是否小于等于0
                if (e.getClickCount()==2 && ((flyingsService ==null&& bulletService == null) || hero.getLifr()<=0)) {
                    score = 0;//获得的分数
                    difficulty = 0;//控制难度
                    isPause = false;//判断是否暂停（false否）
                    hero = new Hero();//创建英雄机对象
                    bulletService = new BulletService();//创建子弹对象;
                    flyingsService = new FlyingsService();//声明一个存储敌机和蜜蜂的数组
                }
            }
        });
        //鼠标监听使英雄机移动
        addMouseMotionListener(new MouseAdapter() {
            /**
             * 监听鼠标在画板内的坐标
             */
            @Override
            public void mouseMoved(MouseEvent e) {
                //判断是否暂停，判断子弹集合和飞行物集合是否为空，判断英雄机血量是否大于0
                if (!isPause && bulletService != null && flyingsService != null && !(hero.getLifr()<=0)){
                    hero.setX(e.getX());
                    hero.setY(e.getY());
                    hero.move();
                }
            }
        });
    }

    /**
     * 画方法
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        sky.paint(g);//画天空
        hero.paint(g);//画英雄机
        //判断子弹集合和飞行物集合是否为空
        if (bulletService != null && flyingsService != null){
            paintHead(g);//画头部
            bulletService.paintBullet(g);//画子弹
            flyingsService.paintFlyings(g);//画飞行物
            //当英雄血量为零时显示结束
            if (hero.getLifr()<=0){
                //画结束界面
                g.drawImage(gameover,0,0,null);
            }
            //判断是否暂停，判断英雄机血量是否大于0
            if (isPause && !(hero.getLifr()<=0)){
                //画暂停界面
                g.drawImage(pause,0,0,null);
            }
        }else {
            //画开始界面
            g.drawImage(start,0,0,null);
        }
    }

    /**
     * 画头部
     * @param g
     */
    public void paintHead(Graphics g){
        g.setColor(new Color(255,255,255));//把笔染成白色
        g.setFont(new Font(null,1,20));//字体大小20
        if (hero.getLifr() <= 0){
            hero.setLifr(0);
            g.drawString("生命:"+hero.getLifr(),5,20);//画生命
        }else {
            g.drawString("生命:"+hero.getLifr(),5,20);//画生命
        }
        if (ShootGame.score <= 0){
            ShootGame.score = 0;
            g.drawString("分数:"+ ShootGame.score,100,20);//画分数
        }else {
            g.drawString("分数:"+ ShootGame.score,100,20);//画分数
        }
        g.drawString("难度:"+ ShootGame.difficulty,300,20);//画难度
    }

    /**
     * 让所有飞行物动起来的方法
     */
    public void action(){
        //创建定时器
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //判断是否暂停，判断子弹集合和飞行物集合是否为空，判断英雄机血量是否大于0
                if (!isPause && bulletService != null && flyingsService != null && !(hero.getLifr()<=0)){
                    //天空移动
                    sky.move();
                    //创建子弹
                    bulletService.newBullet(hero);
                    //创建飞行物
                    flyingsService.newFlyings();
                    //子弹移动
                    bulletService.moveBullet();
                    //飞行物移动
                    flyingsService.moveAction();
                    //子弹和敌机碰撞
                    bulletService.crashAction(flyingsService.getFlyings());
                    //英雄机和敌机蜜蜂的碰撞
                    flyingsService.crashAction(hero);
                }
                repaint();
            }
        } , 100 , 30);
    }

}
